package com.artqiyi.dollmachine.presenter;

import android.text.TextUtils;
import android.widget.Toast;

import com.artqiyi.dollmachine.common.Base.BaseApp;
import com.artqiyi.dollmachine.common.constant.ServerConstant;
import com.artqiyi.dollmachine.presenter.base.BasePresenter;
import com.artqiyi.dollmachine.presenter.base.PresenterCallbackCollection;
import com.artqiyi.dollmachine.utils.Callback.MyStringCallback;
import com.artqiyi.dollmachine.utils.JSONUtils;
import com.artqiyi.dollmachine.utils.WXUtils;
import com.artqiyi.dollmachine.utils.manager.HttpManager;
import com.artqiyi.dollmachine.utils.respone.ConfigRespone;
import com.artqiyi.dollmachine.utils.respone.UserInfoRespone;
import com.artqiyi.dollmachine.views.activity.LoginActivity;
import com.tencent.mm.opensdk.modelmsg.SendAuth;

import okhttp3.Call;
import timber.log.Timber;

/**
 * author: PengCheng
 * time: 2017/12/13 0013
 * desc:
 */

public class LoginPresenter extends BasePresenter {

    public void getUserInfo() {
        HttpManager.getHttpRequest(ServerConstant.getUserInfo(), null, new MyStringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ((PresenterCallbackCollection.LoginActivity) mCallback).onGetUserInfoFail(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                Timber.d("data:" + JSONUtils.getNoteJson(response, "data"));
                if (TextUtils.equals(JSONUtils.getNoteJson(response, "data"), "{}")) {
                    ((PresenterCallbackCollection.LoginActivity) mCallback).onGetTokenInvalid();
                } else {
                    UserInfoRespone userInfoRespone = JSONUtils.jsonString2Bean(response, UserInfoRespone.class);
                    ((PresenterCallbackCollection.LoginActivity) mCallback).onGetUserInfoSuccess(userInfoRespone.getData());
                }
            }
        });
    }

    public void doLogin() {
        //先判断是否安装微信APP,按照微信的说法，目前移动应用上微信登录只提供原生的登录方式，需要用户安装微信客户端才能配合使用。
        if (!WXUtils.isWXAppInstalled(mContext)) {
            Toast.makeText(mContext, "您还未安装微信客户端", Toast.LENGTH_SHORT).show();
            return;
        }
        //微信登录
        SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "diandi_wx_login";
        //像微信发送请求
        BaseApp.mWxApi.sendReq(req);
        ((LoginActivity)mContext).finish();
    }

    public void getConfig() {
        HttpManager.getHttpRequest(ServerConstant.getConfig(), null, new MyStringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ((PresenterCallbackCollection.LoginActivity) mCallback).onConfigFail("请求配置失败");
            }

            @Override
            public void onResponse(String response, int id) {
                Timber.d("response:" + response);
                ConfigRespone configRespone = JSONUtils.jsonString2Bean(response, ConfigRespone.class);
                ((PresenterCallbackCollection.LoginActivity) mCallback).onConfigSuccess(configRespone.getData());
            }
        });
    }
}
